package com.inspinia.base.common.model;

import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;

/**
 * 用户登出事件管理
 * Created by veblen on 2017/2/24.
 */
@Service
public class UserLogoutEventManager {

    private static Collection listeners;

    /**
     * 添加事件
     *
     * @param listener LogoutListener
     */
    public static void addLogoutListener(LogoutListener listener) {
        if (listeners == null) {
            listeners = new HashSet();
        }
        listeners.add(listener);
    }

    /**
     * 移除事件
     *
     * @param listener LogoutListener
     */
    public static void removeLogoutListener(LogoutListener listener) {
        if (listeners == null) {
            return;
        }
        listeners.remove(listener);
    }


    /**
     * 通知所有的LogoutListener
     */
    public static void notifyBeforeLogout() {
        if (null != listeners) {
            Iterator iter = listeners.iterator();
            while (iter.hasNext()) {
                LogoutListener listener = (LogoutListener) iter.next();
                listener.beforeLogout();
            }
        }
    }

}
